import numpy as np
import pandas as pd
import random

def jisuan_shangxiajie(portrait_result, current_mould):
    N, list_len, all_feat, actual_pressure_list, actual_location_list, actual_speed_list, setting_pressure_list, seg_points_list, id_list = \
        portrait_result.load_data_from_good_file(current_mould.good_file)
    df = pd.DataFrame(actual_pressure_list)
    pd1 = df[[i for i in range(N)]]
    zuixiaozhi = pd1.apply(lambda x: x.min(), axis=0)
    zuidazhi = pd1.apply(lambda x: x.max(), axis=0)
    a = list(np.array(zuixiaozhi))
    a1 = [float(i) for i in a]
    b = list(np.array(zuidazhi))
    b1 = [float(i) for i in b]
    # ui.set_shangxiajie_pressure1(b1, a1)

    df1 = pd.DataFrame(actual_speed_list)
    pd2 = df1[[i for i in range(N)]]
    zuixiaozhi_speed = pd2.apply(lambda x: x.min(), axis=0)
    zuidazhi_speed = pd2.apply(lambda x: x.max(), axis=0)
    aa = list(np.array(zuixiaozhi_speed))
    aa1 = [float(i) for i in aa]
    bb = list(np.array(zuidazhi_speed))
    bb1 = [float(i) for i in bb]
    # ui.set_shangxiajie_speed(bb1, aa1)

    return b1, a1, bb1, aa1


# def jisuan_shangxiajie(self, good_file, pressure_junzhi, speed_junzhi):
#     N, list_len, all_feat, actual_pressure_list, actual_location_list, actual_speed_list, setting_pressure_list, seg_points_list, id_list = \
#         self.load_data_from_good_file(good_file)
#     df = pd.DataFrame(actual_pressure_list)
#     pd1 = df[[i for i in range(N)]]
#     zuixiaozhi = np.array(pd1.apply(lambda x: x.min(), axis=0))
#     zuidazhi = np.array(pd1.apply(lambda x: x.max(), axis=0))
#     # 计算差值
#     diff_a = np.array(pressure_junzhi) - zuixiaozhi
#     # print(diff_a)
#
#     # 这里假设我们把c的值更新为均值减去4
#     # a = np.where(diff_a > 5, np.array(pressure_junzhi) - 4, zuixiaozhi)
#     # a1 = [float(i) for i in a]
#     for i in range(len(diff_a)):
#         if diff_a[i] > 5:
#             # 找到与均值相差5以内的一个值，这里简单地选择均值减去5
#             zuixiaozhi[i] = np.array(pressure_junzhi)[i] - random.randint(0, 4)
#     a1 = [float(i) for i in list(zuixiaozhi)]
#
#     # 计算差值
#     diff_b = zuidazhi - np.array(pressure_junzhi)
#     # 使用NumPy的where函数来更新c中的值
#     # 这里假设我们把c的值更新为均值减去5
#     b = np.where(diff_b > 5, np.array(pressure_junzhi) + 4, zuidazhi)
#     b1 = [float(i) for i in b]
#
#     df1 = pd.DataFrame(actual_speed_list)
#     pd2 = df1[[i for i in range(N)]]
#     zuixiaozhi_speed = pd2.apply(lambda x: x.min(), axis=0)
#     zuidazhi_speed = pd2.apply(lambda x: x.max(), axis=0)
#     aa = list(np.array(zuixiaozhi_speed))
#     aa1 = [float(i) for i in aa]
#     bb = list(np.array(zuidazhi_speed))
#     bb1 = [float(i) for i in bb]
#     # ui.set_shangxiajie_speed(bb1, aa1)
#
#     return b1, a1, bb1, aa1
